package cn.bearspark.forward.provider.service.impl;

import cn.bearspark.forward.provider.dao.buffer.FwdHappeningUserIdPageListBuffer;
import cn.bearspark.forward.provider.dao.mapper.HappeningFwdMapper;
import cn.bearspark.forward.provider.service.ForwardService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author f
 */
@Service
public class ForwardServiceImpl implements ForwardService {

    private final HappeningFwdMapper happeningFwdMapper;

    private final FwdHappeningUserIdPageListBuffer fwdHappeningUserIdPageListBuffer;

    public ForwardServiceImpl(HappeningFwdMapper happeningFwdMapper,
                              FwdHappeningUserIdPageListBuffer fwdHappeningUserIdPageListBuffer) {
        this.happeningFwdMapper = happeningFwdMapper;
        this.fwdHappeningUserIdPageListBuffer = fwdHappeningUserIdPageListBuffer;
    }

    @Override
    public boolean forwardHappening(long userId, long happeningId) {
        // 如果转发过了，就不用记录
        if (fwdHappeningUserIdPageListBuffer.isForwarded(userId, happeningId)) {
            return false;
        }

        happeningFwdMapper.insert(userId, happeningId);

        fwdHappeningUserIdPageListBuffer.clean(happeningId);

        return true;
    }

    @Override
    public List<Long> pageHappeningForwardedUserId(int pageNum, int pageSize, long happeningId) {
        return fwdHappeningUserIdPageListBuffer.page(happeningId, pageNum, pageSize);
    }
}
